﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jayden.Dll.Cards.Poker;
using Jayden.Dll.Cards;

namespace Jayden.Console.PokerClient
{
	public class ConsolePokerClient : Jayden.Dll.Cards.Poker.PokerClient
	{
		public ConsolePokerClient() : this("127.0.0.1") { }
		public ConsolePokerClient(string host) : this(host, PokerProtocol.DefaultPort) { }
		public ConsolePokerClient(string host, int port) : base(host, port) { }

		private Dictionary<string, CardSet> m_CardSets = new Dictionary<string, CardSet>();

		public override void OnClientMessage(string message)
		{
			System.Console.WriteLine("CLIENT:> {0}", message);
		}

		public override void OnServerMessage(string message)
		{
			System.Console.WriteLine("SERVER:> {0}", message);
		}

		public override void OnClientConnected()
		{
			System.Console.Write("Login: ");
			string login = System.Console.ReadLine();
			System.Console.Write("Password: ");
			string password = System.Console.ReadLine();

			LogIn(login, password);
		}

		public override void OnClientLogin(string login)
		{
			OnClientMessage(string.Format("Connected as '{0}'.", login));

			string tableName = "OneTable";
			Observe(tableName);
			System.Console.Write("Seat: ");
			int seatIndex = int.Parse(System.Console.ReadLine());
			System.Console.Write("Amount: ");
			ulong amount = ulong.Parse(System.Console.ReadLine());
			Sit(tableName, seatIndex, amount);
		}

		public override void OnClosedCards(string tableName, Dll.Cards.CardList cards)
		{
			m_CardSets.Remove(tableName);
			m_CardSets.Add(tableName, new CardSet(cards));
			System.Console.Write("({0}) received close cards ", tableName);
			System.Console.BackgroundColor = ConsoleColor.Blue;
			System.Console.ForegroundColor = ConsoleColor.White;
			System.Console.Write("[{0}]", cards);
			System.Console.ResetColor();
			System.Console.WriteLine(".");
		}

		public override void OnAction(string tableName, ulong call, ulong raise, ulong allin, int timeOut)
		{
			// todo timeout, currency display, currency input
			if (m_CardSets.ContainsKey(tableName))
				System.Console.WriteLine("You have: {0}.", PokerHighHand.GetBestHand(m_CardSets[tableName]).Description);
			System.Console.Write("Amount for table '{0}' (c:{1},r:{2},a:{3}) : ", tableName, call, raise, allin);
			ulong amount = ulong.Parse(System.Console.ReadLine());
			SendAction(tableName, amount);
		}

		public override void OnShowCards(string tableName, Dll.Cards.CardList cards)
		{
			if (m_CardSets.ContainsKey(tableName))
				m_CardSets[tableName].Add(cards);
			System.Console.Write("({0}) dealer show cards ", tableName);
			System.Console.BackgroundColor = ConsoleColor.DarkGreen;
			System.Console.ForegroundColor = ConsoleColor.White;
			System.Console.Write("[{0}]", cards);
			System.Console.ResetColor();
			System.Console.WriteLine(".");
		}

		public override void OnInitTable(PokerTableView view)
		{
			// should print this ...
		}

		public override void OnClientLoginFail(string login, string reason)
		{
			OnServerMessage("Unable to login with user " + login + " : " + reason + ".");
			OnClientConnected();
		}

		public override void OnLobby(List<PokerTableInfo> cashGames, List<PokerTournamentInfo> tournaments)
		{
			// should print this ...
		}

		public override void OnActionShow(string tableName, int timeOut)
		{
			// todo ...
		}

		public override void OnTableImage(string playerName, System.Drawing.Image image)
		{
			// useless in console mode ?
		}

		public override void OnTimer(string playerName, int seatIndex, int timeOut)
		{
			// useless in console mode ?
		}

		public override void OnException(Exception e)
		{
			// todo ...
		}

		public override void OnClientDisconnected()
		{
			// todo ...
		}

		public override void OnChatMessage(string tableName, string playerName, string message)
		{
			OnClientMessage(string.Format("({0}) {1}: {2}", tableName, playerName, message));
		}

		public override void OnBankroll(ulong bankroll)
		{
			throw new NotImplementedException();
		}

		public override void OnActionDone(string tableName, string playerName, Dll.Cards.Poker.Enums.PokerActionType action, ulong amount)
		{
			// todo ...
		}

		public override void OnTournamentFinished(string tournamentName, int position, ulong amount, PokerCurrency currency)
		{
			// todo ...
		}

		public override void OnBlindGoesUp(string tableName, ulong smallBlind, ulong bigBlind, ulong ante)
		{
			// todo ...
		}

		public override void OnTournamentLobby(string tournamentName, bool isRegistred)
		{
			throw new NotImplementedException();
		}

		public override void OnTournamentRegister(string tournamentName)
		{
			// todo ...
		}

		public override void OnTournamentUnregister(string tournamentName)
		{
			// todo ...
		}

		public override void OnTournamentLobbyPlayers(string tournamentName, List<KeyValuePair<string, ulong>> players)
		{
			throw new NotImplementedException();
		}

		public override void OnTournamentLobbyBettingStructure(string tournamentName, List<PokerTournamentConfiguration.BettingStructureEntry> bettingStructure)
		{
			throw new NotImplementedException();
		}

		public override void OnTournamentLobbyPayoutStructure(string tournamentName, List<PokerTournamentConfiguration.PayoutEntry> payoutStructure)
		{
			throw new NotImplementedException();
		}

		public override void OnActionPost(string tableName, bool smallAndBig, int timeOut)
		{
			// todo ...
		}

		public override void OnAskBuyIn(string tableName, int seatIndex, ulong bankroll, ulong minBuyIn, ulong maxBuyIn)
		{
			throw new NotImplementedException();
		}

		public override void OnTournamentRebuy(string tournamentName, int timeOut)
		{
			// todo ...
		}
	}
}
